﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace WPF.Core.Controls
{
    public class DataPager : Control
    {

        public int PageSize
        {
            get { return (int)GetValue(PageSizeProperty); }
            set { SetValue(PageSizeProperty, value); }
        }

        public int PageNumber
        {
            get { return (int)GetValue(PageNumberProperty); }
            set { SetValue(PageNumberProperty, value); }
        }

        public int PageCount
        {
            get { return (int)GetValue(PageCountProperty); }
            set { SetValue(PageCountProperty, value); }
        }

        public static readonly DependencyProperty PageSizeProperty =
            DependencyProperty.Register("PageSize", typeof(int), typeof(DataPager), new PropertyMetadata(0));

        public static readonly DependencyProperty PageNumberProperty =
            DependencyProperty.Register("PageNumber", typeof(int), typeof(DataPager), new PropertyMetadata(PageNumberChangedCallback));

        private static void PageNumberChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is DataPager pager)
            {
                if (e.OldValue != e.NewValue)
                    pager.UpdateControlsValue();
            }
        }

        public static readonly DependencyProperty PageCountProperty =
            DependencyProperty.Register("PageCount", typeof(int), typeof(DataPager), new PropertyMetadata(PageCountChangedCallback));

        private static void PageCountChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is DataPager pager)
            {
                if (e.OldValue != e.NewValue)
                    pager.UpdateControlsValue();
            }
        }

        private TextBlock txtCount, txtNumber;

        private void UpdateControlsValue()
        {
            if (txtCount != null)
                txtCount.Text = PageCount.ToString();

            if (txtNumber != null)
                txtNumber.Text = PageNumber.ToString();
        }

        public ICommand PageingCommand
        {
            get { return (ICommand)GetValue(PageingCommandProperty); }
            set { SetValue(PageingCommandProperty, value); }
        }

        public static readonly DependencyProperty PageingCommandProperty =
            DependencyProperty.Register("PageingCommand", typeof(ICommand), typeof(DataPager), new PropertyMetadata(null));


        public override void OnApplyTemplate()
        {
            txtCount = (TextBlock)GetTemplateChild("txtCount");
            txtNumber = (TextBlock)GetTemplateChild("txtNumber");

            //https://learn.microsoft.com/zh-cn/uwp/api/windows.ui.xaml.frameworkelement.onapplytemplate?view=winrt-26100
            //            GetTemplateChild("btnHome"): 这是WPF中FrameworkElement类的一个方法，用于获取名为btnHome的模板子元素。btnHome是在控件模板中定义的一个元素的x: Name属性值。
            //is Button btnHome: 这是一个模式匹配语句，用于检查GetTemplateChild("btnHome")返回的对象是否是Button类型。如果是，它将对象转换为Button类型并赋值给btnHome变量。
            if (GetTemplateChild("btnHome") is Button btnHome)
            {
                btnHome.Click += (s, e) => { 
                    PageNumber = 1;
                    PageingCommand?.Execute(null);
                };
            }
            if (GetTemplateChild("btnPrev") is Button btnPrev)
            {
                btnPrev.Click += (s, e) => 
                {
                    if (PageNumber > 1) { PageNumber--; PageingCommand?.Execute(null); }
                };
            }
            if (GetTemplateChild("btnNext") is Button btnNext)
            {
                btnNext.Click += (s, e) => 
                {
                    if (PageNumber == PageCount) return;
                    if (PageNumber > 0) { PageNumber++; PageingCommand?.Execute(null); }
                };
            }
            if (GetTemplateChild("btnEnd") is Button btnEnd)
            {
                btnEnd.Click += (s, e) => {
                    if (PageCount != PageNumber)
                    {
                        PageNumber = PageCount;
                        PageingCommand?.Execute(null);
                    }
                };
            }

            base.OnApplyTemplate();
        }





    }
}
